package Year21_Month10.day1026;

import java.util.Stack;

public class Soultion {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> stack= new Stack();
        if(pushed.length == 0 || popped.length == 0){
            return false;
        }
        int puLdx = 0;//这里定义压栈序列下标
        int poLdx = 0;//这里定义出栈序列下标
        while(puLdx < pushed.length && poLdx < popped.length){
            while(stack.empty() || !(popped[poLdx] != stack.peek())){
                stack.push(pushed[puLdx]);
                puLdx++;
            }
            while(popped[poLdx] == stack.peek()){
                stack.pop();
                poLdx++;
            }
        }
        if(puLdx == poLdx){
            return true;
        }
        return false;
    }

}

 /*这里是关于 括号匹配的练习题
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for(int i = 0;i < s.length();i++){
            char fac = s.charAt(i);
            if(fac == '[' ||
                    fac == '{' ||
                    fac == '('  ){
                stack.push(fac);
            }
            else{
                if(stack.empty()){
                    return false;
                }
                char top = stack.peek();
                if((top == '[' && fac == ']')||
                        (top == '{' && fac == '}')||
                        (top == '(' && fac == ')') ){
                    stack.pop();
                    continue;
                }
                else{
                    return false;
                }
            }
        }
        if(!stack.empty()){
            return false;
        }
        return true;
    }*/
 /*//逆波兰表达式
    public static int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(int i = 0;i < tokens.length;i++){
            if(tokens[i].equals("+")){
                int a = stack.pop();
                int b = stack.pop();
                stack.push(a+b);
            }
            if(tokens[i].equals("-")){
                int a = stack.pop();
                int b = stack.pop();
                stack.push(a-b);
            }
            if(tokens[i].equals("*")){
                int a = stack.pop();
                int b = stack.pop();
                stack.push(a*b);
            }
            if(tokens[i].equals("/")){
                int a = stack.pop();
                int b = stack.pop();
                stack.push(a/b);
            }else{
                stack.push(Integer.valueOf(tokens[i]));
            }
        }
        Integer key = Integer.valueOf(stack.pop());
        return key;
    }*/
